//
//  GameChargeConfirmViewController.m
//  EasyToo
//
//  Created by YuanDee on 13-8-24.
//
//

#import "GameChargeConfirmViewController.h"
#import "GameManager.h"
#import "NetworkAlertView.h"
#import <UPPayPlugin.h>

@interface GameChargeConfirmViewController ()

@end

@implementation GameChargeConfirmViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)initScrollerView
{
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self.view addSubview:_bgScrollView];
    _bgScrollView.contentSize = CGSizeMake(320, 700);
    _bgScrollView.showsVerticalScrollIndicator = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     [self.view setBackgroundColor:BackgroundColor];
    game_model = [GameModel shareData];
    [self initScrollerView];
    UIImage *image = [UIImage imageNamed:@"game_card"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image] ;
    [_bgScrollView addSubview:imageView];
    
	// Do any additional setup after loading the view.
    UILabel *label_message = [[UILabel alloc] initWithFrame:CGRectMake(120, 20, 150, 30)];
    label_message.text = @"信息确认";
    label_message.textColor = [UIColor redColor];
    label_message.font = [UIFont systemFontOfSize:20];
    label_message.backgroundColor = [UIColor clearColor];
    [label_message setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_message];
    
    
    UILabel *label_game = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 250, 30)];
    [label_game setText:[NSString stringWithFormat:@"游戏:%@",game_model.gameName]];
    [label_game setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_game];
    
    
    UILabel *label_price = [[UILabel alloc] initWithFrame:CGRectMake(50, 130, 250, 30)];
    [label_price setText:[NSString stringWithFormat:@"充值金额:%@",game_model.price]];
    [label_price setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_price];
    
    UILabel *label_account = [[UILabel alloc] initWithFrame:CGRectMake(50, 180, 250, 30)];
    [label_account setText:[NSString stringWithFormat:@"充值帐号:%@",game_model.account]];
    [label_account setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_account];
    
    UILabel *label_pay = [[UILabel alloc] initWithFrame:CGRectMake(50, 230, 250, 30)];
    [label_pay setText:[NSString stringWithFormat:@"支付方式 : %@",game_model.payType?@"支付宝":@"银联"]];
    [label_pay setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:label_pay];
    
    UILabel *label_sum = [[UILabel alloc] initWithFrame:CGRectMake(200, 290, 250, 30)];
    [label_sum setText:[NSString stringWithFormat:@"合计 : %@元",game_model.price]];
    label_sum.textColor = [UIColor redColor];
    label_sum.font = [UIFont systemFontOfSize:20];
    label_sum.backgroundColor = [UIColor clearColor];

    [_bgScrollView addSubview:label_sum];
         
    UIButton *confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(40, 350, 240, 40)];
    [confirmButton setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmPay:) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton setTitle:@"确认支付" forState:UIControlStateNormal];
    
    [_bgScrollView addSubview:confirmButton];
}

- (void)confirmPay:(id)sender
{
    [self chargeGame];
//    UIAlertView *av= [[UIAlertView alloc] initWithTitle:@"提示" message:@"未登陆,请先登陆或注册" delegate:self  cancelButtonTitle:@"注册" otherButtonTitles:@"登陆", nil];
//    [av show];
//    [av release];
    //NSLog(@"UIBarButtonItem clicked");
}

-(void) chargeGame
{
    [GameManager shareManager].delegate = self;
    [[GameManager shareManager] chargeGameWithId:game_model.gameName gameUser:game_model.account cardNum:game_model.price];
}

- (void)onChargeGameFinish:(BOOL)isSuccess
                        tn:(NSString*)tn
{
    [[NetworkAlertView shareView] delView];
    NSLog(@"tn:%@", tn);
    [UPPayPlugin startPay:tn sysProvide:nil spId:nil mode:@"00" viewController:self delegate:self];
}

-(void)UPPayPluginResult:(NSString*)result
{
    NSString *alertString = nil;
    if ([result isEqualToString:@"success"])
    {
        alertString = @"交易成功";
    }
    else if ([result isEqualToString:@"cancel"])
    {
        alertString = @"交易取消";
    }
    else
    {
        alertString = @"交易失败";
    }
    
    UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:@"支付结果" message:alertString  delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
